package max.ta.download;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;

import max.ta.util.conf.SystemConfiguration;

import org.apache.log4j.Logger;

// http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
public class DownloaderProxy {

    private static final Logger logger = Logger.getLogger(DownloaderProxy.class);
    private static final String PROXY_HOST = SystemConfiguration.getProxyHost();
    private static final int PROXY_PORT = SystemConfiguration.getProxyPort();

    // restrict instantiation
    private DownloaderProxy() {
	super();
    }

    public static Proxy getProxy() {
	logger.debug("#################################################################");
	logger.debug("# Proxy Host  : " + PROXY_HOST);
	logger.debug("# Proxy Port  : " + PROXY_PORT);
	logger.debug("#################################################################");
	if (isProxyConfigured()) {
	    InetSocketAddress sa = new InetSocketAddress(PROXY_HOST, PROXY_PORT);
	    Proxy proxy = new Proxy(Type.HTTP, sa);
	    return proxy;
	} else {
	    logger.debug("No proxy settings found");
	    return null;
	}

    }

    public static boolean isProxyConfigured() {
	return PROXY_HOST != null || PROXY_PORT != SystemConfiguration.DEFAULT_PROXY_PORT;
    }
}
